﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Data;

namespace Text1
{
    public class Actor
    {
        public Account user;
        public SockedClient client;

        public Actor(Account user, SockedClient client)
        {
            this.user = user;
            this.client = client;
        }
    }
    class ActorManager
    {
        static ActorManager _instance;
        public static ActorManager Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new ActorManager();
                }
                return _instance;
            }
        }

        public Dictionary<string, Actor> actors = new Dictionary<string, Actor>();
        //添加新玩家
        public void Add(Account acc, SockedClient client)
        {         
            actors.Add(acc.username, new Actor(acc, client));
        }
        //移除玩家
        public void Remove(SockedClient client)
        {
            string username = null;
            foreach (var item in actors)
            {
                if (item.Value.client==client)
                {
                    username = item.Key;
                }
            }
            if (username!=null)
            {
                Console.WriteLine(username+"退出游戏");
                Actor actor = actors[username];
                actors.Remove(username);
                GameSever.Instance.SendOther(client, Protocal.LogOut_CND, actor.user);       
            }
        }
        //是否已存在登陆玩家
        public bool HasActor(ReqLogin log)
        {
            if (actors.ContainsKey(log.username))
            {
                return true;
            }
            foreach (var item in actors)
            {
                if (item.Value.user.nickname == log.nickname)
                {
                    return true;
                }
            }
            return false;
        }
        //获取所有玩家的登录信息
        public List<Account>GetAllLogins()
        {
            List<Account> list = new List<Account>();
            foreach (var item in actors)
            {
                list.Add(item.Value.user);
            }
            return list;
        }
        public Account GetActor(string username)
        {
            return actors[username].user;
        }
        public Account GetActor(SockedClient client)
        {
            foreach (var item in actors)
            {
                if (item.Value.client==client)
                {
                    return item.Value.user;
                }
            }
            return null;
        }
    }
}